package com.itheima.web.controller;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.http.HttpResponse;
import com.itheima.domain.db.Admin;
import com.itheima.web.adminmanager.AdminManager;
import com.itheima.web.interceptor.AdminHolder;
import com.itheima.web.usermanager.UserManeger;
import com.sun.org.apache.regexp.internal.RE;
import org.assertj.core.internal.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import java.io.IOException;
import java.util.Map;

@RestController
public class AdminController {
    @Autowired
    private AdminManager adminManager;
    //验证码生成
    @GetMapping("/system/users/verification")
    public void verification(String uuid, HttpServletResponse response) throws IOException {
        //调用rpc
      LineCaptcha lineCaptcha = adminManager.verification(uuid);
        lineCaptcha.write(response.getOutputStream());
    }
    //用户登录
    @PostMapping("/system/users/login")
    public ResponseEntity login(@RequestBody Map<String,String> param){
        String username = param.get("username");
        String password = param.get("password");
        String verificationCode = param.get("verificationCode");
        String uuid = param.get("uuid");
        return adminManager.login(username,password,verificationCode,uuid);
    }
    //用户基本信息
    @PostMapping("/system/users/profile")
    public ResponseEntity findByadmin(){
        Admin admin = AdminHolder.get();
        return ResponseEntity.ok(admin);
    }
    //退出登录
    @PostMapping("/system/users/logout")
    public void logout(@RequestHeader("Authorization") String token){
        adminManager.logout(token);
    }
    @Autowired
    private UserManeger userManeger;
   //用户信息查询
    @GetMapping("/manage/users")
    public ResponseEntity findAllUserinfo(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 调用manager
        return userManeger.findUserInfoByPage(pageNum, pageSize);
    }
    //用户资料查询
    // 用户详情
    @GetMapping("/manage/users/{userId}")
    public ResponseEntity findUserInfoById(@PathVariable Long userId) {
        // 调用manager
        return userManeger.findUserInfoById(userId);
    }
    //用户动态详情
    @GetMapping("/manage/messages")
    public ResponseEntity findMovementVoByPage(
            Integer state,
            @RequestParam(value = "uid", required = false) Long userId,
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 调用manager
        return userManeger.findMovementVoByPage(state, userId, pageNum, pageSize);
    }
    @GetMapping("/manage/messages/{publishId}")
    public ResponseEntity findMovementVoById(@PathVariable String publishId) {
        // 调用manager
        return userManeger.findMovementVoById(publishId);
    }
    //消息列表
    @GetMapping("/manage/messages/comments")
    public ResponseEntity findCommentVoByPage(
            @RequestParam("messageID") String publishId,
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize){
        // 调用manager
        return userManeger.findCommentVoByPage(publishId,pageNum,pageSize);
    }
    //视屏列表
    @GetMapping("/manage/videos")
    public ResponseEntity findVideos(
            @RequestParam("uid") Long userId,
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize
    ){
  return userManeger.findVideos(userId,pageNum,pageSize);
    }

}
